﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using haptic.src.engine.core;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace haptic.src.engine.resource
{
    [Serializable]
    public class Resource
    {
        #region Constructors
        public Resource()
        {

        }
        public Resource(string sFilePath)
        {
            Debug.Assert(sFilePath != null);
            m_sFilePath = sFilePath;
        }
        #endregion

        #region Load
        public static Resource Load(ContentManager oContent, string sPath, Type oType)
        {
            string sExt = Path.GetExtension(sPath);
            Resource oRes = null;
            Stream oStream = new FileStream(S_RESOURCE_DIR + sPath, FileMode.Open);
            if (oStream == null)
            {
                Log.Error("Can't load resource : " + sPath);
                return null;
            }
            if (sExt == ".xml")
            {
                XmlSerializer oSerializer = new XmlSerializer(oType);
                oRes = (Resource)oSerializer.Deserialize(oStream);
            }
            else
            {
                IFormatter oFormatter = new BinaryFormatter();
                oRes = (Resource)oFormatter.Deserialize(oStream);
            }
            oStream.Close();
            if (oRes != null)
            {
                oRes.m_sFilePath = sPath;
                if (oRes.PostLoad(oContent).IsFailed())
                {
                    Log.Error("Cannot PostLoad this resource : " + sPath);
                    oRes = null;
                }
            }
            return oRes;
        }
        public virtual Result PostLoad(ContentManager oContent)
        {
            return Result.Success;
        }
        public Result Save()
        {
            return Save(m_sFilePath);
        }
        public virtual Result Save(string sVirtualPath)
        {
            string sExt = Path.GetExtension(sVirtualPath);
            Stream oStream = new FileStream(S_RESOURCE_DIR + sVirtualPath, FileMode.Create);
            if (oStream == null)
                return new Result("Can't open or create file : " + sVirtualPath);
            if (sExt == ".xml")
            {
                XmlSerializer oSerializer = new XmlSerializer(this.GetType());
                XmlSerializerNamespaces oNs = new XmlSerializerNamespaces();
                oNs.Add("", "");

                oSerializer.Serialize(oStream, this, oNs);
            }
            else
            {
                IFormatter oFormatter = new BinaryFormatter();
                oFormatter.Serialize(oStream, this);
            }
            oStream.Close();
            
            return Result.Success;
        }
        public virtual void Unload()
        {
            m_bIsLoaded = false;
        }
        #endregion

        #region Attributes
        [XmlIgnore]
        public string m_sFilePath;
        private bool m_bIsLoaded = false;
        private const string S_RESOURCE_DIR = "./Content/";
        #endregion
    }
}
